using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_Client.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using DIS_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_Client.DIS_BUS_WS;
	#endregion
	public partial class FrmUpdateOrderDetail : Office2007Form
	{
		public EventHandler Handler { get; set; }
		private DevComponents.DotNetBar.Controls.TextBoxX txtIDItem;

		public FrmUpdateOrderDetail()
		{
			InitializeComponent();
			InitHiddenFields();
		}

		public void InitHiddenFields()
		{
			txtIDItem = new DevComponents.DotNetBar.Controls.TextBoxX
			{
				Name = "txtIDItem",
				Text = ""
			};
			txtIDItem.Visible = false;
		}

		private void btnSelectItem_Click(object sender, EventArgs e)
		{
			FrmManageItem frm = new FrmManageItem(true);
			frm.Handler += GetItem;
			frm.ShowDialog();
		}

		public void GetItem(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
            txtIDItem.Text = comp[0];
			txtItem.Text = comp[1];
            var item = BusService.Instance.Item_SelectByID(int.Parse(comp[0]));
            if (item != null)
            {
                txtPrice.Text = item.Price.ToString();
                var nationalItem = BusService.Instance.NationalPromotion_SelectByIDItem(item.ID);
                if (nationalItem != null)
                {
                    txtDiscount.Text = nationalItem.Discount.ToString();
                }
                else
                {
                    var localItem = BusService.Instance.LocalPromotion_SelectByIDItemAndIDRestaurant(item.ID, GlobalVariables.g_CurrentUser.IDRestaurant);
                    if (localItem != null)
                    {
                        txtDiscount.Text = localItem.Discount.ToString();
                    }
                    else
                    {
                        txtDiscount.Text = string.Empty;
                    }
                }
            }
		}


		private bool ValidateInput(ref string errorText)
		{
			if (txtIDItem.Text == "")
			{
				 errorText = "Please select item";
				return false;
			}
            if (txtQuantity.Text == "")
            {
                errorText = "Please select quantity";
                return false;
            }
            else if(!CommonHelper.IsNumeric(txtQuantity.Text))
            {
                errorText = "Quantity is not numeric";
                return false;
            }
			return true;
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(ref errorText))
			{
				MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			TransferDataInfo(this, new TransferSimpleDataEvent(txtIDItem.Text + "#" + txtQuantity.Text + "#" + txtPrice.Text + "#" + txtDiscount.Text));
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}
		private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}
		private void txtDiscount_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
			{
				e.Handled = true;
			}
		}

	}
}